package com.lqian.share;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class ABA_04 {
    private static AtomicInteger index = new AtomicInteger(10);

    public static void main(String[] args) {
        new Thread(()->{
            // sitmulate A - B - A
            index.compareAndSet(10,11);
            index.compareAndSet(11,10);
            System.out.println(Thread.currentThread().getName()+": 10->11->10");
        },"Kimi").start();

        System.out.println("-----");
        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
                boolean isSuccess = index.compareAndSet(10, 12);
                System.out.println(Thread.currentThread().getName()+": Is the index expected to be 10? ===>"
                            + isSuccess +",The new value is set to " + index.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"Marry").start();
    }
}
